/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.elec.template.web;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.elec.template.entity.ElecTemplate;
import cn.net.ecode.elec.template.service.ElecTemplateService;
import cn.net.ecode.util.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 模板管理Controller
 *
 * @author AlanShelby
 * @version 2019-09-03
 */
@Controller
@RequestMapping(value = "${adminPath}/template/elecTemplate")
public class ElecTemplateController extends BaseController {

    @Autowired
    private ElecTemplateService elecTemplateService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public ElecTemplate get(String id, boolean isNewRecord) {
        return elecTemplateService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("template:elecTemplate:view")
    @RequestMapping(value = {"list", ""})
    public String list(ElecTemplate elecTemplate, Model model) {
        model.addAttribute("elecTemplate", elecTemplate);
        return "elec/template/elecTemplateList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("template:elecTemplate:view")
    @ResponseBody
    @RequestMapping(value = "listData")
    public Page<ElecTemplate> listData(ElecTemplate elecTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ElecTemplate> page = elecTemplateService.findPage(new Page<ElecTemplate>(request, response), elecTemplate);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("template:elecTemplate:view")
    @RequestMapping(value = "form")
    public String form(ElecTemplate elecTemplate, Model model) {
        model.addAttribute("elecTemplate", elecTemplate);
        return "elec/template/elecTemplateForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("template:elecTemplate:edit")
    @ResponseBody
    @RequestMapping(value = "save")
    public String save(ElecTemplate elecTemplate) {
        StringBuilder message = new StringBuilder();
        if (!beanValidator(message, elecTemplate)) {
            return renderResult(Global.FALSE, message);
        }

        // 判断key是否存在
        ElecTemplate template = elecTemplateService.getByTemplateKey(elecTemplate.getTemplateKey());
        if (elecTemplate.getIsNewRecord()) {
            if (template != null) {
                return renderResult(Global.FALSE, "该模版key已存在，请修改！");
            }
        } else {
            ElecTemplate param = new ElecTemplate();
            param.setTemplateKey(elecTemplate.getTemplateKey());
            List<ElecTemplate> templateList = elecTemplateService.findList(param);
            if (templateList.size() >= 2) {
                return renderResult(Global.FALSE, "该模版key已存在，请修改！");
            }
        }

        elecTemplateService.save(elecTemplate);
        return renderResult(Global.TRUE, "保存模板管理成功！");
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("template:elecTemplate:edit")
    @ResponseBody
    @RequestMapping(value = "delete")
    public String delete(ElecTemplate elecTemplate) {
        elecTemplateService.delete(elecTemplate);
        return renderResult(Global.TRUE, "删除模板管理成功！");
    }

}